<?php
/*
 * Copyright (C) 2004-2011 wortzwei GmbH <info@wortzwei.de>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @since 14.01.2011
 * @author Dirk Dittmar
 */

/**
 * Helpers for the SolrSearch Plugin
 * @author Dirk Dittmar
 */
class SolrSearch_Helpers {

	/**
	 * Print the title of a column given its name.
	 * @param string $p_column custom_xxx for custom field xxx, or otherwise field name as in bug table.
	 */
	public static function print_column_title( $p_column) {
		$t_sort = null;
		$t_dir = null;
		$p_columns_target = null;
		
		$t_custom_field = column_get_custom_field_name( $p_column );
		if( $t_custom_field !== null ) {
			echo '<td>';

			$t_field_id = custom_field_get_id_from_name( $t_custom_field );
			if( $t_field_id === false ) {
				echo '@', $t_custom_field, '@';
			} else {
				$t_def = custom_field_get_definition( $t_field_id );
				$t_custom_field = lang_get_defaulted( $t_def['name'] );

				print_view_bug_sort_link( $t_custom_field, $p_column, $t_sort, $t_dir, $p_columns_target );
			}

			echo '</td>';
		} else {
			echo '<td>';
			print_view_bug_sort_link( column_get_title( $p_column ), $p_column, $t_sort, $t_dir, $p_columns_target );
			echo '</td>';
		}
	}

	/**
	 * Print the column values
	 * @param string $p_column column name
	 * @param BugData $p_bug the bug
	 * @param string $p_solr_score_col_name name of the score column
	 */
	public static function print_column_value( $p_column, $p_bug, $p_solr_score_col_name ) {

		$t_column_start = '<td>';
		$t_column_end = '</td>';
		$t_column_empty = '&nbsp;';

		$t_custom_field = column_get_custom_field_name( $p_column );
		if( $t_custom_field !== null ) {
			echo $t_column_start;

			$t_field_id = custom_field_get_id_from_name( $t_custom_field );
			if( $t_field_id === false ) {
				echo '@', $t_custom_field, '@';
			} else {
				$t_issue_id = $p_bug->id;
				$t_project_id = $p_bug->project_id;

				if( custom_field_is_linked( $t_field_id, $t_project_id ) ) {
					$t_def = custom_field_get_definition( $t_field_id );
					print_custom_field_value( $t_def, $t_field_id, $t_issue_id );
				} else {
					// field is not linked to project
					echo $t_column_empty;
				}
			}
			echo $t_column_end;
		} else {
			$t_plugin_columns = columns_get_plugin_columns();

			$t_function = 'print_column_' . $p_column;

			if( function_exists( $t_function ) ) {
				$t_function( $p_bug );
			} else if ( isset( $t_plugin_columns[ $p_column ] ) ) {
				$t_column_object = $t_plugin_columns[ $p_column ];
				print_column_plugin( $t_column_object, $p_bug );

			} else {
				if( isset( $p_bug->$p_column ) ) {
					echo $t_column_start . string_display_line( $p_bug->$p_column ) . $t_column_end;
				} else if( $p_column === $p_solr_score_col_name) {
					echo $t_column_start . $p_bug->solrScore . $t_column_end;
				} else {
					echo $t_column_start . '@' . $p_column . '@' . $t_column_end;
				}
			}
		}
	}

	/**
	 * Printing the pager
	 * @param int $num_pages number of pages available
	 * @param int $page_number actual page number
	 */
	public static function print_page_links( $num_pages, $page_number ) {
		if ($num_pages <= 1) {
			return;
		}

		echo '[ ';
		self::printFirstLink($page_number);
		self::printPrevLink($page_number);

		for ($i = 0; $i < $num_pages; $i++) {
			if ($i + 1 === $page_number) {
				echo $page_number . ' ';
			} else {
				echo self::buildLink($i + 1, $i + 1) . ' ';
			}
		}

		self::printNextLink($num_pages, $page_number);
		self::printLastLink($num_pages, $page_number);
		echo ']';
	}

	private static function printFirstLink($page_number) {
		$first = lang_get( 'first' );
		if ($page_number <= 1) {
			echo $first . ' ';
		} else {
			echo self::buildLink(1, $first) . ' ';
		}
	}

	private static function printLastLink($num_pages, $page_number) {
		$last = lang_get( 'last' );
		if ($page_number >= $num_pages) {
			echo $last . ' ';
		} else {
			echo self::buildLink($num_pages, $last) . ' ';
		}
	}

	private static function printPrevLink($page_number) {
		$prev = lang_get( 'prev' );
		if ($page_number <= 1) {
			echo $prev . ' ';
		} else {
			echo self::buildLink($page_number-1, $prev) . ' ';
		}
	}

	private static function printNextLink($num_pages, $page_number) {
		$next = lang_get( 'next' ) . ' ';
		if ($page_number >= $num_pages) {
			echo $next . ' ';
		} else {
			echo self::buildLink($page_number+1, $next);
		}
	}

	private static function buildLink($page_number, $text) {
		$url = plugin_page( 'search' );
		foreach ($_GET as $key => $value) {
			if ( $key === 'page' || $key === 'page_number' ) {
				continue; // ignore
			}
			$url .= '&' . $key . '=' . $value;
		}
		$url .= '&page_number=' . $page_number;
		return '<a href="' . $url . '">' . $text . '</a>';
	}
}
